var radioUrl = window.location.origin + window.location.pathname + '?subscribe='+topic; 
var clipboard = new ClipboardJS('#address', {
    text: function(trigger) {
        return radioUrl;
    }
}).on('success', function(e) {
   alert('广播地址复制成功');
});
var totalMember = new Array();

// 广播地址设定
$('#address').text('复制广播地址');


client.onMessageArrived = function(message){
    // client.debugMessage(message);

    var receive_topic = message.destinationName;
    var receive_content = message.payloadString;
        receive_content = JSON.parse(receive_content);

    // 更新在线人数
    if(receive_content.totalMember){
        $('#totalMember').text(receive_content.totalMember);
    }

    // 收到聊天记录时
    if(receive_content.message){
        $('.chatContent').append(
            $("<div>",{
                'text':receive_content.message,
                'class':'chatItem'
            }),
        );
        checkKeyword(receive_content.message);
        $('.chatContent').scrollTop($('.chatContent:first')[0].scrollHeight - $('.chatContent:first')[0].clientHeight);

    }

    // 收到消息动作时,触发同步歌曲功能
    if(receive_content.action && receive_content.action=='SYNCSTATUS'){
        var message = JSON.stringify({
            audio:{
                name:player.list.audios[player.list.index].name,
                artist:player.list.audios[player.list.index].artist,
                url:player.list.audios[player.list.index].url,
                cover:player.list.audios[player.list.index].cover
            },
            seek:player.audio.currentTime,
            status: player.audio.paused ? 0 : 1,
            clientId:clientId,
        });
        client.sendMessage(receive_content.topic,message);
    }

    // 收到任何消息时,都推送当前用户状态
    if(receive_content.clientId != clientId){

        // 删除过期内容
        $(totalMember).each(function(index,item){
            if(item.clientId == receive_content.clientId){
                totalMember.splice(index,1);
            }
        });
       
        // 更新过期信息
        totalMember.push({
            'clientId': receive_content.clientId,
            'expired': getTimestamp(10),
        });



        var message = JSON.stringify({
            totalMember:totalMember.length,
            clientId:clientId,
        });
        client.sendMessage(topic,message);
    }
}


// 连接成功,订阅信息
client.onConnectionSuccess = function(message){
    client.debugMessage('服务器连接成功');
    client.subscribe(topic);
}


// 初始化播放列表
$(getRandomSong('周杰伦')).each(function(index,song){
    if(index < 10){
        player.list.add(
            [{
                name: song.name,
                artist: song.singer,
                url: 'https://api.bbbug.com/api/song/playurl?mid='+song.mid,
                cover: song.pic
            }]
        );
    }
});

// 广播端播放器播放事件监听
player.on('play',function(){
    var message = JSON.stringify({
        audio:{
            name:player.list.audios[player.list.index].name,
            artist:player.list.audios[player.list.index].artist,
            url:player.list.audios[player.list.index].url,
            cover:player.list.audios[player.list.index].cover
        },
        seek:player.audio.currentTime,
        status: 1,
        clientId:clientId,
    });
    client.sendMessage(topic,message);
});

// 广播端播放器进度条事件监听
player.on('seeked',function(){
    var message = JSON.stringify({
        audio:{
            name:player.list.audios[player.list.index].name,
            artist:player.list.audios[player.list.index].artist,
            url:player.list.audios[player.list.index].url,
            cover:player.list.audios[player.list.index].cover
        },
        seek:player.audio.currentTime,
        status: player.audio.paused ? 0 : 1,
        clientId:clientId,
    });
    client.sendMessage(topic,message); 
});

// 广播端播放器暂停事件监听
player.on('pause',function(){
    var message = JSON.stringify({
        audio:{
            name:player.list.audios[player.list.index].name,
            artist:player.list.audios[player.list.index].artist,
            url:player.list.audios[player.list.index].url,
            cover:player.list.audios[player.list.index].cover
        },
        seek:player.audio.currentTime,
        status: 0 ,
        clientId:clientId,
    });
    client.sendMessage(topic,message); 
});


// 聊天
$('.chatInput').on('keydown',function(e){
    _keyword = $(this).val();
    if(e.keyCode==13 || e.keyCode==108){
        $(this).val('');
        var message = JSON.stringify({
            'clientId':clientId,
            'message':_keyword,
        });

        client.sendMessage(topic,message);
    }
});


// 歌单申请 - 通过
$('#primary').on('click','.approval',function (e) {
    var songDiv =  $(this).parents('li:first');
    player.list.add([{
        name: $(songDiv).attr('data-name'),
        artist: $(songDiv).attr('data-artist'),
        url: $(songDiv).attr('data-url'),
        cover: $(songDiv).attr('data-cover')
    }]);

    player.list.switch(player.list.audios.length-1);
    player.pause();
    player.play();
    $(songDiv).remove();
}); 

// 歌单申请 - 拒绝
$('#primary').on('click','.reject',function (e) {
    $(this).parents('li:first').remove();
}); 

// 定时监测客户端状态
setInterval(function(){
    $(totalMember).each(function(index,item){
        if(parseInt(item.expired) < parseInt(getTimestamp(0))){
            console.log('删除过期' + clientId);
            totalMember.splice(index,1);
        }
    });
    // 校验客户端存活
    var message = JSON.stringify({
        totalMember: totalMember.length,
        clientId:clientId
    });
    client.sendMessage(topic,message);

    console.log('校验客户端连接数量');
},10000);



